package org.example.chat.dao;

import org.example.chat.entity.RegionInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RegionInfoRepository extends JpaRepository<RegionInfo, String> {

    List<RegionInfo> findByName(String areaName);

    @Query(value = "from #{#entityName} ri where ri.level = :level order by ri.sort asc")
    List<RegionInfo> findByLevel(@Param("level") String level);

    @Query(value = "from #{#entityName} ri where ri.superiorCode = :code order by ri.sort asc")
    List<RegionInfo> findBySuperiorCode(@Param("code") String code);

    @Query(value = "select * from  region_info where cri_superior_code = (SELECT t1.cri_code FROM `region_info` t1 where t1.cri_name = ?1) and cri_data_state = 1 order by cri_level asc", nativeQuery = true)
    List<RegionInfo> findByParentName(String name);
}
